global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/original_estimates.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {

* Some programs to help create the original estimates table
cap program drop init_columns

*prepare matrix and column anmes
program define init_columns
	mat A = J(9,6,.)
	matrix colnames A = estimateL standard_errorL standard_errorL_country estimateH standard_errorH standard_errorH_country
	global columns ""
	global i 0
end

*runs a columns regression and adds regression coefficients and standard errorsto the matrix created in init_columns
cap program drop run_column
program run_column
	syntax varlist(ts) [if], absorb(varlist) column(string)
	global columns "$columns `column'"
	global i = $i + 1

	ppmlhdfe `varlist' `if', absorb(`absorb') vce(cluster lse_id)
	matrix b = e(b)
	matrix V = e(V)
	ppmlhdfe `varlist' `if', absorb(`absorb') vce(cluster country_shr_1995)
	matrix Vc = e(V)


	mat A[$i,1] = b[1,1]
	mat A[$i,2] = sqrt(V[1,1])
	mat A[$i,3] = sqrt(Vc[1,1])

	local second_coefficient : word 2 of `e(indepvars)'

	if strpos("`second_coefficient'","HSW") {
		mat A[$i,4] = b[1,2]
		mat A[$i,5] = sqrt(V[2,2])
		mat A[$i,6] = sqrt(Vc[2,2])	
	}

end

*save the table as CSV. Why like this and not directly tex? because we use it in addition to other values that were created. this lets us add them in the same final table
cap program drop save_table
program define save_table
	syntax using [, replace]
	matrix rownames A = $columns
	esttab matrix(A) `using',  plain csv margin b(%10.2f) se(%10.2f) nolines noeqlines nogaps sfmt(a1) type nomtitles nonumbers `replace'
	
	preserve
	import delimited `using', clear varnames(1) case(preserve)
	ren v1 column
	export delimited `using', `replace'
end program


** Load data

use ${final_dir}/regression_dataset_from1970_tfacit1.dta, clear


*the usual cloning and renaming
tab year, gen(YY_)
estimates clear
clonevar spilloversown = spill${depvar}${ttt}_1995_a
clonevar spilloversother = spillN${depvar}${ttt}_1995_a
clonevar spilloversownzero = spill${depvar}${ttt}_1995_a0
clonevar spilloversotherzero = spillN${depvar}${ttt}_1995_a0
clonevar stockown = k${depvar}_${ttt} 
clonevar stockownzero = k${depvar}_${ttt}0  
clonevar stockother = kNOT_${depvar}_${ttt} 
clonevar stockotherzero = kNOT_${depvar}_${ttt}0
clonevar LSW = lswMPm_1995_a
clonevar HSW = hswMPm_1995_a
clonevar VAEMP = vaempMPm_1995_a
clonevar GDPGAP = lngdpgap_1995_a
clonevar GDPPC = gdppcMPm_1995_a
bys lse_id: egen _total_${depvar}_${ttt}_1995 = sum(${depvar}_${ttt}) if year>=1995+2 & year<=2009+2
bys lse_id: egen total_${depvar}_${ttt}_1995 = max(_total_${depvar}_${ttt}_1995)
drop _total_${depvar}_${ttt}_1995
egen yearctry = group(year country_shr_1995) if year <= 2009
egen yearindustry = group(year industry) if year <= 2009
sort lse_id year


init_columns

* 1) GDPGAP | F+IY
run_column F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) column("1")

* 2) GDPGAP + VAEMP | F+IY
run_column F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) column("2")

* 3) GDPGAP + GDPPC | F+IY
run_column F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry) column("3")

* 4) GDPGAP | F+IY+CY
run_column F2.${depvar}_${ttt} LSW HSW GDPGAP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) column("4")

* 5) GDPGAP + VAEMP | F+IY+CY
run_column F2.${depvar}_${ttt} LSW HSW GDPGAP VAEMP stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) column("5")

* 6) GDPGAP + GDPPC | F+IY+CY
run_column F2.${depvar}_${ttt} LSW HSW GDPGAP GDPPC stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0, absorb(lse_id yearindustry yearctry) column("6")

clonevar GDPPC_foreign = gdppcMPm_shr4_foreign_1995_a	
clonevar VAEMP_foreign = vaempMPm_shr4_foreign_1995_a
clonevar GDPgap_foreign = lngdpgap_shr_foreign_1995_a
clonevar LSW_foreign = lswMPm_shr4_foreign_1995_a
clonevar HSW_foreign = hswMPm_shr4_foreign_1995_a

* 7) GDPGAP | F+IY+CY | foreign
run_column F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) column("7")

* 8) GDPGAP + VAEMP | F+IY+CY | foreign
run_column F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) column("8")

* 9) GDPGAP + GDPPC | F+IY+CY | foreign
run_column F2.${depvar}_${ttt} LSW_foreign HSW_foreign GDPgap_foreign GDPPC_foreign stockown stockownzero stockother stockotherzero spilloversown spilloversownzero spilloversother spilloversotherzero if year>=1995 & missing_weights_1995==0 & missing_spill_weights_1995 == 0 & maxweight_1995 < 1 & total_${depvar}_${ttt}_1995>0,absorb(lse_id yearindustry yearctry) column("9")

drop LSW_foreign HSW_foreign GDPgap_foreign VAEMP_foreign GDPPC_foreign
drop stockown stockownzero stockother stockotherzero
drop spilloversown spilloversownzero spilloversother spilloversotherzero


save_table using ${dataset_dir}/montecarlo/original_estimates.csv, replace	

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat
